<?php 
	class MainView
	{
		public $access;
		public $prefix = 'timetable_';
		public $user;
		public function __construct(){
			global $wpdb;
			$this->user = $this->getUser();
			$this->access = $this->user['role_status'];

			switch ($this->access) {
				case 1:
					$this->access = 1;
					break;
				case 2:
					$this->access = 1;
					break;
				case 3:
					$this->access = 3;
					break;
				case 4:
					$this->access = 3;
					break;
				case 5:
					$this->access = 5;
					break;
				default:
					$this->access = 6;
					break;
			}

		}

		public function getUser(){
			global $wpdb;
			$prefix = $this->prefix;
			$current_user = wp_get_current_user(); 
			$id = $current_user->data->ID;
			$query_user = "SELECT iduser id, role_name, role_status FROM {$prefix}user, {$prefix}role WHERE wp_user_id = $id AND idrole=role_id";
			$user_res = $wpdb->get_row($query_user, ARRAY_A);
			return $user_res ;
		}

		public function getFields(){
			
			$prefix = $this->prefix;
			$fields_array = array(
					'get_all'=>array(
							'access'=>6,
							'query'=>array(
									"1"=>"SELECT  idclientcourse,clientcourse_lessontotal  FROM timetable_clientcourse",
									"2"=>"SELECT  
			  						  idclientcourse, 
									  course_id,
									  course_name,
									  course_basecost,
									  paystatus_id,
									  paystatus_name,
									  teacher_id,
									  teacher_name,
									  clientcourse_lessontotal,
									  clientcourse_count,
									  client_id,
									  client_name,
									  formoftraining_id,
									  formoftraining_name,
									  DATE_FORMAT(clientcourse_begin,'%d-%m-%Y') clientcourse_begin 
									 FROM {$prefix}clientcourse, {$prefix}course, {$prefix}client, {$prefix}paystatus, {$prefix}teacher 
									 WHERE course_id = idcourse AND paystatus_id = idpaystatus AND teacher_id = idteacher AND client_id = idclient AND "
								)
							),
					'insert'=>array(
							'access'=>3
						),
					'update'=>array(
							'access'=>3
						),
					'delete'=>array(
							'access'=>3
						)
			);
			
			return $fields_array;
		}

		public function findAll(){
			global $wpdb;
			$prefix = $this->prefix;
			$DEBUG = true;

			if($DEBUG){
				$query = "SELECT  
					idclientcourse,
					clientcourse_lessontotal,
					course_id,
									course_name,
									course_basecost,
									paystatus_id,
									paystatus_name,
									teacher_id,
									teacher_name,
									clientcourse_lessontotal,
									clientcourse_count,
									client_id,
									client_name,
									formoftraining_id,
									formoftraining_name,
									DATE_FORMAT(clientcourse_begin,'%d-%m-%Y') clientcourse_begin 	  
								FROM {$prefix}clientcourse, {$prefix}course, {$prefix}client, {$prefix}paystatus, {$prefix}teacher 
								WHERE course_id = idcourse AND paystatus_id = idpaystatus AND teacher_id = idteacher AND client_id = idclient AND idformoftraining = formoftraining_id";
				$results = $wpdb->get_results($query, ARRAY_A);
				return $query;
			}else{
				switch ($this->access) {
					case 1:
						$query = "SELECT  
									idclientcourse,
									clientcourse_lessontotal,
									course_id,
									course_name,
									course_basecost,
									paystatus_id,
									paystatus_name,
									teacher_id,
									teacher_name,
									clientcourse_lessontotal,
									clientcourse_count,
									client_id,
									client_name,
									formoftraining_id,
									formoftraining_name,
									DATE_FORMAT(clientcourse_begin,'%d-%m-%Y') clientcourse_begin 	  
								FROM {$prefix}clientcourse, {$prefix}course, {$prefix}client, {$prefix}paystatus, {$prefix}teacher 
								WHERE course_id = idcourse AND paystatus_id = idpaystatus AND teacher_id = idteacher AND client_id = idclient AND idformoftraining = formoftraining_id";
						$results = $wpdb->get_results($query, ARRAY_A);
						return $results;
						break;
					case 3:
						$this->access = 3;
						break;
					case 5:
						$this->access = 5;
						break;
					default:
						$this->access = 6;
						break;
				}
			}


			//$results = $wpdb->get_results($query, ARRAY_A);
			//return $results;
		}	
	}		
?>